<script setup>
import { useRouter } from 'vue-router'
import ChatBot from '@/components/chat-bot/chat-bot.vue'
import { ref, onUnmounted, onMounted } from 'vue'

const userInfo = ref({})

// 监听唤醒的参数
const stopAwakenWatch = window.ipcRenderer?.on?.('awaken', (params) => {
  console.log(params, 'urlparams')
  handleAwaken(params)
})

const handleAwaken = (params) => {
  window.sessionStorage.clear('awaken')
  try {
    let _params = typeof params === 'string' ? JSON.parse(params) : params

    if (_params?.name) {
      userInfo.value = _params
    } else {
      userInfo.value = {}
    }
  } catch (err) {
    console.log(err)
    userInfo.value = {}
  }
}

onMounted(() => {
  // 防止从url唤醒app时，vue还没装载已经触发了 awaken 事件，导致第一次无法监听到
  handleAwaken(window.sessionStorage.getItem('awaken'))
})

onUnmounted(() => {
  stopAwakenWatch?.()
})
</script>

<template>
  <div class="home">
    <ChatBot :userInfo="userInfo" />
  </div>
</template>

<style lang="scss" scoped>
.home {
  width: 100%;
  height: 100%;
}
</style>
